// pages/member/card/list/index.js
import {
  request
} from '../../../../request';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardList: [],
    modalName: null,
    applyTime:1,
    selectItem:null
  },
  onShow: function () {
    this.initData();
  },
  initData() {
    request("/gym/vip/list", "GET").then(resopnse => {
      resopnse.rows.forEach(item => item["flag"] = this.checkTime(item));
      console.log(resopnse)
      this.setData({
        cardList: resopnse.rows
      });
    })
  },
  checkTime(row) {
    console.log(row);
    var effective = row.effective.replace("-", "/"); //替换字符，变成标准格式
    var today = new Date();
    var date = new Date(Date.parse(effective));
    if (today < date) return true;
    else return false;
  },
  pageBack() {
    wx.navigateBack({
      delta: 1
    });
  }, // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },
  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },
  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
  handleSignIn(e) {
    var that = this;
    var item = e.currentTarget.dataset['item'];
    request("/gym/vip/sign/in/"+item.vipId, "GET").then(res => {
      if (res.code == "200") {
        wx.showToast({
          title: '签到成功',
          icon: '',
          duration: 2000,
        })
      }
    })
  },
  onChange(e) {
    this.setData({
      applyTime: e.detail.value
    })
    console.log(this.data);
  },
  showModal(e) {
    console.log(e);
    this.setData({
      modalName: e.currentTarget.dataset.target,
      selectItem:e.currentTarget.dataset.item
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null,
      selectItem:null
    })
  },
  renewal(){
    console.log(this.data.selectItem);
    var data = this.data.selectItem;
    if(this.data.applyTime!=null) data.renewal = this.data.applyTime;
    var that = this;
    request("/gym/vip/renewal", "POST",data).then(res => {
      if (res.code == "200") {
        that.setData({
          modalName: null,
          selectItem:null
        })
        that.initData();
        wx.showToast({
          title: '续费成功',
          icon: '',
          duration: 2000,
        })
      }
    })
  },
  DateChange(e) {
    var item = this.data.selectItem;
    item.effective = e.detail.value;
    item.renewal = null,
    this.setData({
      applyTime: null,
      selectItem : item
    })
  },
})